/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.toedo.controllers.DataController;
import org.toedo.gui.wizards.newtask.NewTaskWizard;
import org.toedo.logging.Log;
import org.toedo.model.Task;

public class NewTaskAction extends Action {
		
	private Shell mShell;
	private String mTitle;
	private boolean mOpenTask;
	
	public NewTaskAction(Shell shell, boolean openTask) {
		mShell = shell;
		mTitle = null;
		mOpenTask = openTask;
	}
	
	public NewTaskAction(Shell shell, String title, boolean openTask) {
		mShell = shell;
		mTitle = title;
		mOpenTask = openTask;
	}
	
	public void run() {
		if ((mTitle == null) ||
				(mTitle.isEmpty())) {
			NewTaskWizard wizard = new NewTaskWizard(true);		
			WizardDialog wizardDialog = new WizardDialog(mShell, wizard);		
			wizardDialog.open();
		} else {
			Task newTask = DataController.getInstance().getProject().addTask(mTitle);			
			Log.getInstance(NewTaskAction.class).debug(String.format("Task created: %s (%s)", newTask.getTitle(), newTask.getId())); //$NON-NLS-1$
			
			if ((mOpenTask) &&
					(newTask != null)) {
				new OpenTaskAction(newTask).run();
			}
		}				
	}

}
